﻿using System;
using System.Configuration;
using FileSecurityManager.DataAccess.Exceptions;
using log4net;

namespace FileSecurityManager.DataAccess.Repositories
{
    public class Linq2SqlRepository: IDisposable
    {
        #region class members
        private const string FsmStorageConnectionString = "FSMStorageConnectionString";
        protected static ILog Log;
        #endregion

        #region properties

        /// <summary>
        /// Gets or sets the FSM database.
        /// </summary>
        /// <value>The FSM database.</value>
        public FSMStorageDataContext FSMDatabase { get; set; }
        #endregion

        #region constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Linq2SqlRepository"/> class.
        /// </summary>
        public Linq2SqlRepository()
        {
            string connectionString;

            if (ConfigurationManager.ConnectionStrings[FsmStorageConnectionString] == null)
            {
                throw new DataAccessException(
                   string.Format(ExceptionMessages.NoConnectionStringInConnStringSection,
                    FsmStorageConnectionString));
            }

            try
            {
                connectionString = ConfigurationManager.ConnectionStrings[FsmStorageConnectionString].ConnectionString;
            }
            catch (ConfigurationErrorsException ex)
            {
                throw new DataAccessException(
                    string.Format(ExceptionMessages.CouldNotReadConnectionString,
                    FsmStorageConnectionString, ex.Message), ex);
            }

            if (String.IsNullOrEmpty(connectionString))
            {
                throw new DataAccessException(
                       string.Format(ExceptionMessages.ConnectionStringIsEmpty,
                        FsmStorageConnectionString));
            }

            FSMDatabase = new FSMStorageDataContext(connectionString);

            Log = LogManager.GetLogger(GetType());
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Linq2SqlRepository"/> class.
        /// </summary>
        /// <param name="connectionString">The connection string.</param>
        public Linq2SqlRepository(string connectionString)
        {
            if (String.IsNullOrEmpty(connectionString))
            {
                throw new DataAccessException(
                    ExceptionMessages.RepositoryInitFailed,
                        new ArithmeticException(ExceptionMessages.ConnectionStringEmpty));
            }

            FSMDatabase = new FSMStorageDataContext(connectionString);
            Log = LogManager.GetLogger(GetType());
        } 
        #endregion

        #region IDisposable Members

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            FSMDatabase.Dispose();
        }
        #endregion
    }
}
